/* enum */
import StorageKeyEnum from '@model/enum/StorageKeyEnum'
import ThemeClassEnum from '@model/enum/ThemeClassEnum'
import StorageModuleEnum from '@model/enum/StorageModuleEnum'
/* util */
import { removeClassByRegExp } from '@src/util/dom';
import { storageGet, storageSet } from '@src/util/storage.ts'

/**
 * 切换整体主题 （在iframe外层调用）
 * @param {string} themeClass 主题className
 */
export const setThemeClass = async function(themeClass: ThemeClassEnum = ThemeClassEnum.Default) {
  let iframes : any = document.getElementsByTagName('iframe');
  let parentBody : any = document.getElementsByTagName('body')[0];
    
  removeClassByRegExp(parentBody, /theme-*/);
  parentBody.classList.add(themeClass);
  iframes.forEach((item : any) => {
    const url = new URL(item.src);
    if(url.hostname === window.location.hostname){
      let childBody = item.contentWindow.document.getElementsByTagName('body')[0];
      removeClassByRegExp(childBody, /theme-*/);
      childBody.classList = [themeClass];
    }
  });
  // await storageSet(StorageKeyEnum.ThemeClass, themeClass, StorageModuleEnum.Common);
}

/** 
 * 获取当前主题
 * @returns {string} 当前主题
 * */
export const getThemeClass = async function() {
  return 'theme-default'
}